#include "saxparser.h"

SaxParser :: SaxParser(VecContinentes &vec, string &rutaI, VecMisiones &vm) : 
	vContinentes(vec),
	rutaImagen(rutaI),
	vMisiones(vm){
	
}

void SaxParser :: on_start_element(const Glib::ustring& name,
                                const AttributeList& properties){
	  if(name.compare("continente") == 0){
	  		cargaContinente(properties);
	  }
	  else if(name.compare("pais") == 0){
	  		cargaPais(properties);
	  	}
	  else if(name.compare("conexion") == 0){
	  		cargaConeccion(properties);
	  }
	  else if(name.compare("ruta") == 0){
		  xmlpp::SaxParser::AttributeList::const_iterator iter = properties.begin();
		  rutaImagen.assign(iter->value);
	  }
	  else if(name.compare("conquista") == 0){
		  cargaConquista(properties);
	  }
	  else if(name.compare("eliminacion") == 0){
	  	  cargaEliminacion(properties);
	  }
}

void SaxParser :: cargaContinente(const AttributeList& properties){
	string nombre;
	int bonus;
	
	for(xmlpp::SaxParser::AttributeList::const_iterator iter = properties.begin(); iter != properties.end(); ++iter){
		if(iter->name.compare("nombre") == 0){
			nombre.assign(iter->value);
			continente.assign(iter->value);
		}
		else if(iter->name.compare("bonus") == 0){
			string bon;
			bon.assign(iter->value);
			bonus = atoi(bon.c_str());
		}
	}
	vContinentes.add(nombre,bonus);
}

void SaxParser :: cargaPais(const AttributeList& properties){
	string nombre;
	bool mPais;
	int radio, x, y;
	
	for(xmlpp::SaxParser::AttributeList::const_iterator iter = properties.begin(); iter != properties.end(); ++iter){
		if(iter->name.compare("nombre") == 0){
			nombre.assign(iter->value);
			pais.assign(iter->value);
			
		}
		else if(iter->name.compare("radio") == 0){
				radio = atoi(iter->value.c_str());
			}
			else if(iter->name.compare("x") == 0){
					x = atoi(iter->value.c_str());
				}
				else if(iter->name.compare("y") == 0){
						y = atoi(iter->value.c_str());
					}
				else if(iter->name.compare("mostrar") == 0){
					if(iter->value.compare("true")){
						mPais = true;
					}
					else mPais = false;
				}
	}
	
	Continente *c = vContinentes.get_Continente(continente);
	c->agregarPais(nombre,radio, x, y , mPais, *c, vContinentes);
}

void SaxParser :: cargaConeccion(const AttributeList& properties){
	
	string nPais, nContinente;
	
	for(xmlpp::SaxParser::AttributeList::const_iterator iter = properties.begin(); iter != properties.end(); ++iter){
		if(iter->name.compare("nombre") == 0){
			nPais.assign(iter->value);
		}
		else if(iter->name.compare("continente") == 0){
				nContinente.assign(iter->value);
			}
	}
	
	Continente *c = vContinentes.get_Continente(continente);
	Pais *p = c->get_Pais(pais);
	p->agregar_Coneccion(nPais, nContinente);
}

void SaxParser :: cargaConquista(const AttributeList& properties){
	
	string continente, s_extras, descripcion;
	int extras;
	
	for(xmlpp::SaxParser::AttributeList::const_iterator iter = properties.begin(); iter != properties.end(); ++iter){
		if(iter->name.compare("continente") == 0){
			continente.assign(iter->value);
		}
		else if(iter->name.compare("descripcion") == 0){
				descripcion.assign(iter->value);
		}
		else if(iter->name.compare("extras") == 0){
			s_extras.assign(iter->value);
			extras = atoi(s_extras.c_str());
		}
	}
	vMisiones.agregarMision(continente, descripcion, extras);
}

void SaxParser :: cargaEliminacion(const AttributeList& properties){
	
	string color, descripcion;
	int extras;
	
	for(xmlpp::SaxParser::AttributeList::const_iterator iter = properties.begin(); iter != properties.end(); ++iter){
		if(iter->name.compare("color") == 0){
			color.assign(iter->value);
		}
	}
	vMisiones.agregarMision(color);
}
